package wnhz.com.business.uitls;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import wnhz.com.business.MyApplication;


/**
 * Created by wanghongchuang
 * on 2016/8/25.
 * email:844285775@qq.com
 */
public class Prefer {
    private static final String TAG = "Prefer";
    private static final String PREFERENCE_FILE = "syb";

    private static Prefer mInstance;
    private SharedPreferences mPref;
    private SharedPreferences.Editor editor;

    private final String KEY_TOKEN = "KEY_TOKEN";
    private final String KEY_PHONE = "KEY_PHONE";
    private final String KEY_NAME = "KEY_NAME";
    private final String KEY_L0GINTYPE = "KEY_LOGINTYPE";
    private final String KEY_IMAGE = "KEY_IMAGE";
    private final String KEY_REGTYPE = "KEY_REGTYPE";
    private final String KEY_NEEDGUIDE = "KEY_NEEDGUIDE";
    private final String KEY_PASSWORD = "KEY_PASSWORD";
    private final String KEY_lOGINTYPE = "KEY_lOGINTYPE";
    private final String KEY_USER_ID = "KEY_USER_ID";


    public static Prefer getInstance() {
        if (null == mInstance) {
            mInstance = new Prefer();
        }
        return mInstance;
    }

    private Prefer() {
        mPref = MyApplication.getInstance().getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
        editor = mPref.edit();
    }


    //token
    public void setToken(String token) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_TOKEN, token);
        editor.commit();
    }

    public String getToken() {
        return mPref.getString(KEY_TOKEN, "");
    }

    //user_id
    public void setUser_id(String User_id) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_USER_ID, User_id);
        editor.commit();
    }

    public String getUser_id() {
        return mPref.getString(KEY_USER_ID, "");
    }

    //手机号
    public void setPhone(String phone) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PHONE, phone);
        editor.commit();
    }

    public String getPhone() {
        return mPref.getString(KEY_PHONE, "");
    }

    //密码
    public void setPwd(String pwd) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PASSWORD, pwd);
        editor.commit();
    }

    public String getPwd() {
        return mPref.getString(KEY_PASSWORD, "");
    }


    //名称
    public void setName(String name) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_NAME, name);
        editor.commit();
    }

    public String getName() {
        return mPref.getString(KEY_NAME, "");
    }

    //登录类型（密码or短信）
    public void setloginTypeS(String name) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_lOGINTYPE, name);
        editor.commit();
    }

    public String getloginTypeS() {
        return mPref.getString(KEY_lOGINTYPE, "");
    }










    //登录类型  1 手机号  2 微信  3qq
    public void setLoginType(String loginTypoe) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_L0GINTYPE, loginTypoe);
        editor.commit();
    }

    public String getLoginType() {
        return mPref.getString(KEY_L0GINTYPE, "");
    }


    public String getImage() {
        return mPref.getString(KEY_IMAGE, "");
    }

    public void setImage(String profileImage) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_IMAGE, profileImage);
        editor.commit();
    }


    public void setRegType(String regType) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_REGTYPE, regType);
        editor.commit();
    }

    //登录方式
    public String getRegType() {
        return mPref.getString(KEY_REGTYPE, "");
    }


    //引导页

    public void setNeedGuide(boolean needGuide) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(KEY_NEEDGUIDE, needGuide);
        editor.commit();
    }

    public boolean getneedGuide() {
        return mPref.getBoolean(KEY_NEEDGUIDE, true);
    }


    public void putObject(String key, Object object) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            editor.putString(key, objectVal);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public <T> T getObject(String key, Class<T> clazz) {
        if (mPref.contains(key)) {
            String objectVal = mPref.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    //退出登录后清除缓存数据
    public void clearData() {
        boolean needguide = getneedGuide();

        //清楚数据
        SharedPreferences.Editor editor = mPref.edit();
        editor.clear();
        editor.commit();

        //重新写入
        setNeedGuide(needguide);
    }
}
